package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.bo.ChatSessionBo;
import org.dromara.chat.domain.vo.ChatSessionVo;
import org.dromara.chat.service.ChatSessionService;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天会话控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/session")
public class ChatSessionController extends BaseController {

    private final ChatSessionService chatSessionService;

    /**
     * 查询聊天会话列表
     */
    @SaCheckPermission("chat:session:list")
    @GetMapping("/list")
    public TableDataInfo<ChatSessionVo> list(ChatSessionBo bo, PageQuery pageQuery) {
        return chatSessionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天会话列表
     */
    @SaCheckPermission("chat:session:export")
    @Log(title = "聊天会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatSessionBo bo, HttpServletResponse response) {
        List<ChatSessionVo> list = chatSessionService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天会话", ChatSessionVo.class, response);
    }

    /**
     * 获取聊天会话详细信息
     */
    @SaCheckPermission("chat:session:query")
    @GetMapping("/{id}")
    public R<ChatSessionVo> getInfo(@PathVariable Long id) {
        return R.ok(chatSessionService.queryById(id));
    }

    /**
     * 新增聊天会话
     */
    @SaCheckPermission("chat:session:add")
    @Log(title = "聊天会话", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ChatSessionBo bo) {
        return toAjax(chatSessionService.insertByBo(bo));
    }

    /**
     * 修改聊天会话
     */
    @SaCheckPermission("chat:session:edit")
    @Log(title = "聊天会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ChatSessionBo bo) {
        return toAjax(chatSessionService.updateByBo(bo));
    }

    /**
     * 删除聊天会话
     */
    @SaCheckPermission("chat:session:remove")
    @Log(title = "聊天会话", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(chatSessionService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据用户ID查询会话列表
     */
    @SaCheckPermission("chat:session:query")
    @GetMapping("/user/{userId}")
    public R<List<ChatSessionVo>> getByUserId(@PathVariable Long userId) {
        return R.ok(chatSessionService.queryListByUserId(userId));
    }

    /**
     * 创建新会话
     */
    @SaCheckPermission("chat:session:add")
    @Log(title = "聊天会话", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R<ChatSessionVo> createNewSession(@RequestParam Long userId, @RequestParam(required = false) String sessionTitle) {
        return R.ok(chatSessionService.createNewSession(userId, sessionTitle));
    }

    /**
     * 更新会话标题
     */
    @SaCheckPermission("chat:session:edit")
    @Log(title = "聊天会话", businessType = BusinessType.UPDATE)
    @PutMapping("/{sessionId}/title")
    public R<Void> updateSessionTitle(@PathVariable Long sessionId, @RequestParam String sessionTitle) {
        return toAjax(chatSessionService.updateSessionTitle(sessionId, sessionTitle));
    }

}